package com.StarStudio.Controller;

import com.StarStudio.Service.RateService;
import com.ruoyi.common.core.Entity.HttpStatusCode;
import com.ruoyi.common.core.Entity.MessageConstant;
import com.ruoyi.common.core.Entity.Rate;
import com.ruoyi.common.core.Entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/rate")
@Api(tags = "志愿者服务时长和鲜豆积分的兑换率接口")
public class RateController {

    @Autowired
    RateService rateService;

    /**
     * 新增志愿者服务时长和鲜豆积分的兑换率接口
     * @return 结果对象
     * @Author ChengLee
     * @Date 2023/04/19
     */
    @PostMapping
    @ApiOperation("新增志愿者服务时长和鲜豆积分的兑换率接口 -> 兑换率不可为空")
    public Result addRate(@RequestBody Rate rate){
        if (rate.getRate() == null){
            return new Result(HttpStatusCode.BAD_REQUEST, MessageConstant.NOT_NULL_PARAMETER_ERROR);
        }
        try {
            rateService.addRate(rate);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.ADD_RATE_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.ADD_RATE_SUCCESS);
    }

    /**
     * 获取当前志愿者服务时长和鲜豆积分兑换率
     * @return 结果
     * @Author ChengLee
     * @Date 2023/04/19
     */
    @GetMapping
    @ApiOperation("获取当前志愿者服务时长和鲜豆积分兑换率接口")
    public Result getCurrentRate(){
        Rate currentRate;
        try {
            currentRate = rateService.findCurrentRate();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.GET_CURRENT_RATE_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.GET_CURRENT_RATE_SUCCESS,currentRate);
    }

    /**
     * 获取志愿者服务时长和鲜豆积分兑换率列表
     * @param rate 志愿者服务时长和鲜豆积分兑换率
     * @return 结果
     * @Author ChengLee
     * @Date 2023/04/19
     */
    @GetMapping("/list")
    @ApiOperation("获取志愿者服务时长和鲜豆积分兑换率列表接口（支持条件检索）")
    public Result getCurrentRate(Rate rate){
        List<Rate> rateList;
        try {
            rateList = rateService.findRateList(rate);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.GET_RATE_LIST_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.GET_RATE_LIST_SUCCESS,rateList);
    }

}
